package com.specialtreasure.freightdepot.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.app.core.config.Constants;
import com.app.core.config.HttpApi;
import com.app.core.util.UserUtils;
import com.app.core.fragment.BaseNewFragment;
import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.specialtreasure.freightdepot.R;
import com.specialtreasure.freightdepot.adapter.MotorCadeAdapter;
import com.specialtreasure.freightdepot.model.MotorCade;
import com.specialtreasure.freightdepot.model.MotorCadeBean;
import com.specialtreasure.freightdepot.model.MotorCadeData;
import com.specialtreasure.freightdepot.popupwindow.SelectUnitPW;
import com.yanzhenjie.httpmodule.http.CallServer;
import com.yanzhenjie.httpmodule.http.Result;
import com.yanzhenjie.httpmodule.http.StringRequest;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.CacheMode;
import com.yanzhenjie.nohttp.rest.Response;
import com.yanzhenjie.nohttp.rest.SimpleResponseListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/7 0007.
 * 我的车队单页面 3个tab
 */

public class ShareMotorCadeFragment extends BaseNewFragment implements View.OnClickListener {

    public static final String TAG = "ShareMotorCadeFragment";
    private TextView tv_car_length;
    private RelativeLayout rl_car_length;
    private TextView tv_car_type;
    private RelativeLayout rl_car_type;
    private TextView tv_path;
    private RelativeLayout rl_path;
    private ListView lv_moto_cade;
    private PtrClassicFrameLayout ptr_layout;

    String token;
    String carLength;//车长
    String carType;//车型
    String runningRoute;//常跑路线(目的地)
    String runningRouteStart;//常跑路线(出发地)
    Integer count = 10;//每页条数
    Integer page = 1;//页数

    List<MotorCadeBean> motorCadeDataList = new ArrayList<MotorCadeBean>();
    MotorCadeAdapter motorCadeAdapter;
    Boolean hasMode;
    private LinearLayout ll_root;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(mContext, R.layout.frag_moto_cade_three, null);
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setData();
    }

    private void setData() {

        //初始化自动刷新
        ptr_layout.postDelayed(new Runnable() {
            @Override
            public void run() {
                ptr_layout.autoRefresh(true);
            }
        }, 150);

        //下拉刷新
        ptr_layout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                ptr_layout.setLoadMoreEnable(true);
                page = 1;
                motorCadeDataList.clear();
                getData();
            }
        });

        ptr_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                if (!hasMode) {
                    page++;
                    getData();
                } else {
                    Toast.makeText(mContext, "没有更多数据了", Toast.LENGTH_SHORT).show();
                    ptr_layout.setLoadMoreEnable(false);
                }
            }
        });
    }

    /**
     * 网络请求
     */
    private void getData() {
        token = UserUtils.getToken();
        String url = Constants.HTTP_URL + HttpApi.SHARECARLIST;
        StringRequest request = new StringRequest(url, RequestMethod.POST);
        request.add("token", token);
        request.add("count", count);
        request.add("page", page);

        request.addNull("carLength", carLength);
        request.addNull("carType", carType);
        request.addNull("runningRouteStart", runningRouteStart);
        request.addNull("runningRoute", runningRoute);


        request.setCacheMode(CacheMode.ONLY_REQUEST_NETWORK);

        CallServer.getInstance().request(Constants.ZERO, request, new SimpleResponseListener<Result<String>>() {
            @Override
            public void onSucceed(int what, Response<Result<String>> response) {
                super.onSucceed(what, response);
                ptr_layout.refreshComplete();
                ptr_layout.loadMoreComplete(true);

                String result = response.get().getResult();
                Logger.e("请求成功：" + result);
                MotorCade motorCade = JSON.parseObject(result, MotorCade.class);

                if (motorCade != null) {
                    Integer status = motorCade.getStatus();
                    String msg = motorCade.getMsg();
                    //判断状态是
                    if (status != null && status == 0) {
                        MotorCadeData motorCadeData = motorCade.getData();
                        if (motorCadeData != null) {
                            hasMode = motorCadeData.getHasMore();
                            //适配数据
                            List<MotorCadeBean> motorCadetList = motorCadeData.getResultList();

                            motorCadeDataList.addAll(motorCadetList);
                            if (motorCadeAdapter == null) {
                                motorCadeAdapter = new MotorCadeAdapter(getActivity());
                                motorCadeAdapter.setMotorCadeDataList(motorCadeDataList);
                                lv_moto_cade.setAdapter(motorCadeAdapter);
                            } else {
                                motorCadeAdapter.setMotorCadeDataList(motorCadeDataList);
                                motorCadeAdapter.notifyDataSetChanged();
                            }

                        }

                    } else {
                        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
                    }
                }

            }
        });
    }


    private void initView(View view) {
        tv_car_length = (TextView) view.findViewById(R.id.tv_car_length);
        rl_car_length = (RelativeLayout) view.findViewById(R.id.rl_car_length);
        tv_car_type = (TextView) view.findViewById(R.id.tv_car_type);
        rl_car_type = (RelativeLayout) view.findViewById(R.id.rl_car_type);
        tv_path = (TextView) view.findViewById(R.id.tv_path);
        rl_path = (RelativeLayout) view.findViewById(R.id.rl_path);
        lv_moto_cade = (ListView) view.findViewById(R.id.lv_moto_cade);
        ptr_layout = (PtrClassicFrameLayout) view.findViewById(R.id.ptr_layout);

        rl_car_length.setOnClickListener(this);
        rl_car_type.setOnClickListener(this);
        rl_path.setOnClickListener(this);
        ll_root = (LinearLayout) view.findViewById(R.id.ll_root);
        ll_root.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_car_length://车长
                SelectUnitPW supw = new SelectUnitPW(getActivity(), "chechang.xml", "车长");
                supw.showAtBottom(ll_root);
                supw.setOnResultListener(new SelectUnitPW.OnResultListener() {
                    @Override
                    public void onResult(String str) {
                        if (!TextUtils.isEmpty(str)) {
                            tv_car_length.setText(str);
                            carLength = str;
                            getData();
                        }
                    }
                });
                break;
            case R.id.rl_car_type://车型
                SelectUnitPW supw1 = new SelectUnitPW(getActivity(), "chexing.xml", "车型");
                supw1.showAtBottom(ll_root);
                supw1.setOnResultListener(new SelectUnitPW.OnResultListener() {
                    @Override
                    public void onResult(String str) {
                        if (!TextUtils.isEmpty(str)) {
                            tv_car_type.setText(str);
                            carType = str;
                            getData();
                        }
                    }
                });
                break;
            case R.id.rl_path://长跑路线

                break;
        }
    }

    @Override
    protected String getFragmentName() {
        return TAG;
    }
}
